package com.example.drug.controller;

import com.example.drug.domain.Sales;
import com.example.drug.service.SalesService;
import com.example.drug.util.AjaxResult;
import com.example.drug.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售记录Controller
 *
 * @author [你的名字]
 * @date [具体日期]
 */
@RestController
@RequestMapping("/api/sales")
@Api(tags = "药店销售记录管理609余函池")
public class SalesController {

    @Autowired
    private SalesService salesService;

    /**
     * 获取全部销售记录列表
     */
    @ApiOperation("获得全部销售记录列表")
    @GetMapping("/saleslist")
    public AjaxResult getAllSalesList() {
        return AjaxResult.success(salesService.selectAllSales());
    }

    /**
     * 根据条件查询销售记录列表（此处以买家编号为例，可按需扩展更多条件）
     */
    @ApiOperation("根据条件查询销售记录列表")
    @ApiImplicitParam(name = "buyerId", value = "买家编号",
            dataType = "long", dataTypeClass = Long.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "buyerId", required = false) Long buyerId) {
        List<Sales> list = salesService.selectSalesByParam(buyerId);
        return AjaxResult.success(list);
    }

    /**
     * 获取销售记录详细信息
     */
    @ApiOperation("获取销售记录详细信息")
    @ApiImplicitParam(name = "id", value = "销售记录ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Sales sales = salesService.selectSalesById(id);
        if (sales == null || sales.getSaleId() == null) {
            throw new BizException(404, "销售记录 " + id + " 不存在");
        }
        return AjaxResult.success(sales);
    }

    /**
     * 新增销售记录
     */
    @ApiOperation("新增销售记录")
    @PostMapping
    public AjaxResult add(@RequestBody Sales sales) {
        return AjaxResult.success(salesService.insertSales(sales));
    }

    /**
     * 修改销售记录
     */
    @ApiOperation("修改销售记录")
    @PutMapping
    public AjaxResult edit(@RequestBody Sales sales) {
        return AjaxResult.success(salesService.updateSales(sales));
    }

    /**
     * 删除销售记录
     */
    @ApiOperation("删除销售记录")
    @ApiImplicitParam(name = "id", value = "销售记录ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(salesService.deleteSalesById(id));
    }
}